package zmee.pmas.report;

import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.events.PackageDataEvent;
import kd.bos.entity.report.FilterInfo;
import kd.bos.entity.report.ReportQueryParam;
import kd.bos.form.control.Control;
import kd.bos.form.field.BasedataEdit;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.report.events.SummaryEvent;
import kd.bos.report.plugin.AbstractReportFormPlugin;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import zmee.util.DateUtil;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author yss
 * @createTime 2023-01-05 16:04
 * @description 项目执行情况报表表单插件
 */
public class ProExecuteShowFormPlugin extends AbstractReportFormPlugin implements BeforeF7SelectListener {
    @Override
    public void registerListener(EventObject e) {
        BasedataEdit pro = this.getControl("pro");
        pro.addBeforeF7SelectListener(this);
    }

    @Override
    public void setMergeColums(List<String> columns) {
        super.setMergeColums(columns);
        columns.add("project");
    }
    @Override
    public void initDefaultQueryParam(ReportQueryParam queryParam) {
        Object[] org=new Object[]{RequestContext.get().getOrgId()};
        this.getModel().setValue("org", org);
    }

    @Override
    public void setFloatButtomData(List<SummaryEvent> summaryEvents) {
        super.setFloatButtomData(summaryEvents);
    }

    @Override
    public void packageData(PackageDataEvent packageDataEvent) {
        this.getModel().getEntryEntity("reportlistap");      
        super.packageData(packageDataEvent);
    }

    @Override
    public void processRowData(String gridPK, DynamicObjectCollection rowData, ReportQueryParam queryParam) {

        Map<String,Map<String,BigDecimal>> map=new HashMap();

        for (DynamicObject row : rowData) {
            Map<String,BigDecimal> count=new HashMap();
            String berz = row.getString("berz");
            String projectId = row.getString("project.id");
            if("小计".equals(berz)){
                continue;
            }
            BigDecimal monthpay = row.getBigDecimal("monthpay");//合同金额
            BigDecimal yearpay = row.getBigDecimal("yearpay");//合同金额
            BigDecimal totalamt = row.getBigDecimal("totalamt");//合同金额
            BigDecimal originalo = row.getBigDecimal("originalo");//合同金额
            BigDecimal amountoftax = row.getBigDecimal("amountoftax");//补充合同金额
            BigDecimal ftaxamount = row.getBigDecimal("ftaxamount");//预估签证金额
            BigDecimal finalsettle = row.getBigDecimal("finalsettle");//决算金额
            BigDecimal yearbillamt = row.getBigDecimal("yearbillamt");//本年到票金额
            BigDecimal totalbillamt = row.getBigDecimal("totalbillamt");//累计到票金额
            Map<String,BigDecimal> countMap = map.get(projectId);
            if(countMap!=null){
                originalo = originalo.add(countMap.get("originalo"));
                amountoftax = amountoftax.add(countMap.get("amountoftax"));
                ftaxamount = ftaxamount.add(countMap.get("ftaxamount"));
                finalsettle = finalsettle.add(countMap.get("finalsettle"));
                monthpay = monthpay.add(countMap.get("monthpay"));
                yearpay = yearpay.add(countMap.get("yearpay"));
                yearbillamt = yearbillamt.add(countMap.get("yearbillamt"));
                totalamt = totalamt.add(countMap.get("totalamt"));
                totalbillamt = totalbillamt.add(countMap.get("totalbillamt"));

            }
            count.put("originalo",originalo);
            count.put("amountoftax",amountoftax);
            count.put("ftaxamount",ftaxamount);
            count.put("finalsettle",finalsettle);
            count.put("monthpay",monthpay);
            count.put("yearpay",yearpay);
            count.put("yearbillamt",yearbillamt);
            count.put("totalamt",totalamt);
            count.put("totalbillamt",totalbillamt);
            map.put(projectId,count);
        }


        for (DynamicObject row:rowData) {
            String berz = row.getString("berz");
            String projectId = row.getString("project.id");
            if("小计".equals(berz)){
                Map<String,BigDecimal> countMap = map.get(projectId);
                BigDecimal yearpay = countMap.get("yearpay");//本年项目累计支付金额
                BigDecimal totalbillamt = countMap.get("totalbillamt");//项目累计到票金额
                BigDecimal totalamt = countMap.get("totalamt");//累计支付金额
                BigDecimal mount = row.getBigDecimal("mount");//年度预算金额
                BigDecimal bugamt = row.getBigDecimal("bugamt");//预算总金额
                row.set("originalo",countMap.get("originalo"));
                row.set("amountoftax",countMap.get("amountoftax"));
                row.set("ftaxamount",countMap.get("ftaxamount"));
                row.set("finalsettle",countMap.get("finalsettle"));
                row.set("monthpay",countMap.get("monthpay"));
                row.set("yearpay",yearpay);
                row.set("yearbillamt",countMap.get("yearbillamt"));
                row.set("totalamt",countMap.get("totalamt"));
                row.set("totalbillamt",totalbillamt);

                if(!"0E-10".equals(mount.toString())){
                    BigDecimal mountBi = yearpay.divide(mount, 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("100")).setScale(2,BigDecimal.ROUND_HALF_UP);
                    row.set("cute",mountBi+"%");
                }else{
                    row.set("cute","00%");
                }

                if(!"0E-10".equals(bugamt.toString())) {
                    BigDecimal bugamtBi = totalamt.divide(bugamt, 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("100")).setScale(2, BigDecimal.ROUND_HALF_UP);
                    row.set("total", bugamtBi + "%");
                }else{
                    row.set("cute", "00%");
                }
            }
        }

    }

    @Override
    public void beforeF7Select(BeforeF7SelectEvent event) {
        String name = event.getProperty().getName();
        if("pro".equals(name)){
            Object org = this.getModel().getValue("org");
            if(org != null) {
                String ids = ids((DynamicObjectCollection) org);
                List<QFilter> list = new ArrayList<>();
                QFilter qFilter = new QFilter("useorg.id", QCP.in,ids);
                list.add(qFilter);
                event.setCustomQFilters(list);
            }
        }
    }

    private String ids(DynamicObjectCollection row){
        String proIds="";
        for (int i = 0; i < row.size(); i++) {
            if(!"".equals(proIds) && proIds != null){
                proIds=proIds+",";
            }
            proIds = proIds+row.get(i).getString("fbasedataid.id");
        }
        return proIds;
    }
}
